package com.su.gulimall.search.thread;

import java.util.concurrent.*;

public class ActualDuty {
    public static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,200,10,
            TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        });
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "黑色+256G";
        });
        CompletableFuture<String> futureBrandName = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的牌子");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        });
        //异步编排
        CompletableFuture<Void> future = CompletableFuture.allOf(futureImg, futureAttr, futureBrandName);
        //等待所有结果完成,才进行值的获取
        future.join();
        System.out.println(futureImg.get()+futureBrandName.get()+futureAttr.get());
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureBrandName);
        anyOf.get();
        System.out.println(anyOf.get());
    }
}
